# coding=utf-8
'''
公共方法.
'''
import decimal
import json
import os
import random

import time


def json_loads(text):
    try:
        return json.loads(text)
    except Exception:
        return None


def get_time_stamp():
    return str(int(time.time() * 1000))


def get_cur_time():
    return time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime())


def get_pid_log_name(log_name):
    return log_name + '_' + str(os.getpid()) + '.log'


def sub_time(cur_time, pre_time):
    from datetime import datetime
    cur_localtime = datetime.strptime(cur_time, '%Y-%m-%d %H:%M:%S')
    pre_localtime = datetime.strptime(pre_time, '%Y-%m-%d %H:%M:%S')
    return (cur_localtime - pre_localtime).seconds


def get_now_time():
    from datetime import datetime
    return datetime.now().strftime("%Y-%m-%d %H:%M:%S")


def get_gm_time():
    return time.strftime(u"%a %b %d %Y %H:%M:%S GMT+0800 (CST)", time.gmtime(time.time() + 8 * 60 * 60))


def get_random_num():
    x = [random.random()]
    decimal.getcontext().prec = 16
    return decimal.Decimal(x[0]) * 1


# 过滤无效数据
def check_html(text):
    if text.find('无效用户') != -1 and text.find('Unauthorized') != -1:
        return False
    return True
